const Blog = require('../schema/blog.js').Blog;


const findall = (data, skipnum, pageSize, sort) => {
    return new Promise((resolve, reject) => {
        Blog.find(data).skip(skipnum).limit(pageSize).sort(sort).exec(function (err, doc) {
            if (err) {
                reject(err)
            }
            resolve(doc)
        })
    })
}

const findOne = (data) => {
    return new Promise((resolve, reject) => {
        Blog.findOne(data, (err, doc) => {
            if (err) {
                reject(err)
            }
            resolve(doc)
        })
    })
}


const findnormal = (data,limit) => {
    return new Promise((resolve, reject) => {
        Blog.find(data).limit(limit).exec((err, doc) => {
            if (err) {
                reject(err)
            }
            resolve(doc)
        })
    })
}
const countall = (data) => {
    return new Promise((resolve, reject) => {
        Blog.count(data, (err, doc) => {
            if (err) {
                reject(err)
            }
            resolve(doc)
        })
    })
}

const insertone = function (data) {
    return new Promise((resolve, reject) => {
        Blog.create(data, (err) => {
            if (err) {
                reject(err)
            }
            resolve()
        })
    })
}
const dellist = function (id) {
    return new Promise((resolve, reject) => {
        Blog.findOneAndRemove(id, err => {
            if (err) {
                reject(err);
            }
         
            resolve();
        });
    });
};

const doedit = function (id, newdata) {
    return new Promise((resolve, reject) => {


        Blog.update(id, newdata, err => {
            if (err) {
                reject(err);
            }
            
            resolve();
        })
    })
}



module.exports = {
    findall,
    findnormal,
    insertone,
    dellist,
    doedit,
    findOne,
    countall
}